/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-29 15:59:46
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:37:14
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { BomContentServiceProxy, BomEditList, BomEditParentData, BomDesignDetailDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent, TreeNode } from 'primeng/api';

@Component({
  selector: 'app-bom-watch',
  templateUrl: './bom-watch.component.html',
  styleUrls: ['./bom-watch.component.less']
})
export class BomWatchComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    codeFlag = false;
    treeData: any;
    selectedNode: any;
    parentTitle = '';
    sonTitle = '';
    rowdata = [];
    oldTree: any;
    selectRow: any;
    bomUsing = false;
    sonData: any;
    secondSon: any;
    form = { materialCode: '', materialname: '', model: '' };
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _fileDownloadService: FileDownloadService,
    ) {
        super(injector);
    }
    ngOnInit() {
    }
    selectStatus(value) {
        switch (value) {
            case 0:
                return '编制中';
            case 1:
                return '审核中';
            case 2:
                return '被驳回';
            case 3:
                return '已发布';
            case 4:
                return '已维护';
            case 5:
                return '待维护';
            default:
                return null;
        }
    }
    //保存任务
    save(): void {
    }
    //打开弹窗
    onShown(): void {
        this.getTreedata();
    }
    //查看新增任务
    show(): void {
        if (this.title == 'BOM查看') {
            this.initEditInfo();
        } 
        this.active = true;
        this.modal.show();
    }
    //初始化修改数据
    initEditInfo() {
        this.getTreedata();
    }
    getTreedata() {
        this._bomContentServiceProxy.getBomChildTreeGrid(
            this.editData.btId,
            2
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.treeData = [];
                this.treeData.push(result);
                this.treeData[0].data.levelNum = 3;
                this.form.model = result.data.model;
                this.form.materialCode = result.data.materialNo;
                this.form.materialname = result.data.materialName;
                this.expandAll();
            });
    }
    expandAll() {
        for (const node of this.treeData) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        }
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form.model = undefined;
        this.form.materialCode = undefined;
        this.form.materialname = undefined;
        this.active = false;
        this.treeData = [];
        this.rowdata = [];
        this.oldTree = [];
        this.parentTitle = undefined;
        this.sonTitle = undefined;
        this.modal.hide();
    }

}
